package com.mvp.lt.airlineview.location;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.graphics.Color;
import android.view.LayoutInflater;

import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polyline;
import com.mvp.lt.airlineview.App;
import com.mvp.lt.airlineview.R;
import com.mvp.lt.airlineview.databinding.DroneMiddlePointMarkerBinding;
import com.mvp.lt.airlineview.databinding.DronePointMarkerBinding;
import com.mvp.lt.airlineview.map.MapCalculationLogLatDistance;
import com.mvp.lt.airlineview.utils.LogUtil;
import com.mvp.lt.airlineview.map.MapCreateManager;
import com.mvp.lt.airlineview.utils.MainThread;
import com.mvp.lt.airlineview.utils.ThreadUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author LiuTao
 * @description: 画地图航线相关类
 * @date : 2020/11/9 10:28
 */
public class WayPointLineMapUtils {
    private List<LatLng> latLngDataWaypoints = null;
    private List<Marker> markerWaypoints = null;
    private List<Marker> markerMiddleWaypoints = null;
    private List<Polyline> polylineWaypoints = null;
    private static WayPointLineMapUtils waypointLineUtils;
    private Marker currentMarker = null;
    private Marker currentMiddleMarker;
    private LatLng currentLatLng;
    private Polyline currentPolyline;


    public Context getContext() {
        return App.getContext();
    }

    public static WayPointLineMapUtils getInstance() {
        if (waypointLineUtils == null) {
            waypointLineUtils = new WayPointLineMapUtils();
        }
        return waypointLineUtils;
    }

    public WayPointLineMapUtils() {
        initData();
    }

    private void initData() {
        markerWaypoints = new ArrayList<>();
        markerMiddleWaypoints = new ArrayList<>();
        latLngDataWaypoints = new ArrayList<>();
        polylineWaypoints = new ArrayList<>();
    }

    /**
     * 反转航线
     */
    public void reversalData() {
        Collections.reverse(latLngDataWaypoints);
        Collections.reverse(markerWaypoints);
        Collections.reverse(polylineWaypoints);
        Collections.reverse(markerMiddleWaypoints);
        reversalWayPointMarker();
        reversalPlusSignWayPointMarker();
    }

    /**
     * 删除航点
     */
    public void deleteCurrentData() {
        if (currentMarker != null && markerWaypoints.size() > 0) {//删除当前选中的航点
            int currentPosition = currentMarker.getPeriod();
            int totalSize = markerWaypoints.size();

            //判断 marker 索引
            if (currentPosition == totalSize) {//结束点
                //航线Marker点索引
                int currentIndex = currentPosition - 1;
                //带加号的Marker点索引和航线的索引
                int plusSignIndex = currentPosition - 2;
                LogUtil.e("Marker", "@@@@@@位置：" + currentPosition + "数量：" + markerWaypoints.size());//
                //删除当前航点Marker点
                currentMarker.remove();
                currentMarker = null;
                markerWaypoints.remove(currentIndex);
                latLngDataWaypoints.remove(currentIndex);
                if (markerMiddleWaypoints.size() > 0) {
                    //删除带加号的Marker
                    Marker marker = markerMiddleWaypoints.get(plusSignIndex);
                    marker.remove();
                    markerMiddleWaypoints.remove(plusSignIndex);
                }
                if (polylineWaypoints.size() > 0) {
                    //删除线段
                    Polyline polyline = polylineWaypoints.get(plusSignIndex);
                    polyline.remove();
                    polylineWaypoints.remove(plusSignIndex);
                }
                if (markerWaypoints.size() > 0) {
                    currentMarker = markerWaypoints.get(markerWaypoints.size() - 1);
                    currentPosition = currentMarker.getPeriod();
                    totalSize = markerWaypoints.size();
                    changeCheckWaypointCurrentMarkerStatusColor(getContext(), totalSize, currentMarker, currentPosition);
                }
            } else if (currentPosition == 1) {//起始点
                //航线Marker点索引
                int currentIndex = currentPosition - 1;
                //带加号的Marker点索引和航线的索引
                int afterIndex = currentPosition - 1;
                LogUtil.e("Marker", "######位置：" + currentPosition + "数量：" + markerWaypoints.size());//
                //删除当前航点Marker点
                currentMarker.remove();
                currentMarker = null;
                markerWaypoints.remove(currentIndex);
                latLngDataWaypoints.remove(currentIndex);
                //删除第一个带加号的点
                if (markerMiddleWaypoints.size() > 0) {
                    //删除带加号的Marker
                    Marker marker = markerMiddleWaypoints.get(afterIndex);
                    marker.remove();
                    markerMiddleWaypoints.remove(afterIndex);
                }
                //删除线段
                if (polylineWaypoints.size() > 0) {
                    Polyline polyline = polylineWaypoints.get(afterIndex);
                    polyline.remove();
                    polylineWaypoints.remove(afterIndex);
                }
                if (markerWaypoints.size() > 0) {
                    //排序
                    sortWayPointMarkerByClickPosition(getContext(), currentPosition - 2, markerWaypoints);
                }
                if (markerMiddleWaypoints.size() > 0) {
                    //排序
                    sortAddSignWayPointMarkerByClickPosition(currentPosition, markerMiddleWaypoints);
                }
                if (markerWaypoints.size() > 0) {
                    currentMarker = markerWaypoints.get(0);
                    totalSize = markerWaypoints.size();
                    changeCheckWaypointCurrentMarkerStatusColor(getContext(), totalSize, currentMarker, 1);
                }

            } else { //中间航点
                //航线Marker点索引  3
                int afterIndex = currentPosition - 1; //2
                //带加号的Marker点索引和航线的索引
                int frontIndex = currentPosition - 2;//1
                //todo
                LogUtil.e("Marker", "$$$$$$位置：" + currentPosition + "数量：" + markerWaypoints.size());//
                //删除当前航点Marker点
                currentMarker.remove();
                currentMarker = null;
                //删除航点
                markerWaypoints.remove(afterIndex);
                //删除航点经纬度
                latLngDataWaypoints.remove(afterIndex);
                //前后两航点
                LatLng latLng1 = latLngDataWaypoints.get(frontIndex);
                LatLng latLng2 = latLngDataWaypoints.get(afterIndex);
                //前一个航点方向
                Marker frontWayPointMarker = markerWaypoints.get(frontIndex);
                double azimuthAngle = MapCalculationLogLatDistance.getAzimuthAngle(latLng1.latitude, latLng1.longitude, latLng2.latitude, latLng2.longitude);
                frontWayPointMarker.setSnippet(azimuthAngle + "");
                //删除带加号的航点 两个点
                markerMiddleWaypoints.get(afterIndex).remove();
                markerMiddleWaypoints.remove(afterIndex);
                markerMiddleWaypoints.get(frontIndex).remove();
                markerMiddleWaypoints.remove(frontIndex);
                //画出新的带加号的点
                Marker middlePlusMarker = addMiddleMarker(getContext(), latLng1, latLng2, frontIndex);
                markerMiddleWaypoints.add(frontIndex, middlePlusMarker);
                //线
                polylineWaypoints.get(afterIndex).remove();
                polylineWaypoints.remove(afterIndex);
                polylineWaypoints.get(frontIndex).remove();
                polylineWaypoints.remove(frontIndex);
                //画出新的航线
                Polyline polyline = MapCreateManager.getMapManager().drawTwoPointBlueLine(latLng1, latLng2);
                polylineWaypoints.add(frontIndex, polyline);
                //排序  从当前的序号开始 如果currentPosition =3，
                sortWayPointMarkerByClickPosition(getContext(), currentPosition - 3, markerWaypoints);
                sortAddSignWayPointMarkerByClickPosition(currentPosition - 1, markerMiddleWaypoints);
                //
                if (markerWaypoints.size() > 0) {
                    currentMarker = markerWaypoints.get(currentPosition - 1);
                    totalSize = markerWaypoints.size();
                    changeCheckWaypointCurrentMarkerStatusColor(getContext(), totalSize, currentMarker, currentPosition);
                }
            }
        }
    }

    /**
     * 反转航线航点 Marker
     */
    public void reversalWayPointMarker() {
        if (markerWaypoints.size() < 1) {
            return;
        }
        int currenPosition = 0;
        if (currentMarker != null) {
            currenPosition = currentMarker.getPeriod();// 3  --- 3-3 =0;
        }
        //反转
        currenPosition = markerWaypoints.size() - currenPosition;
        //当前选中的Marker点
        for (int i = 0; i < markerWaypoints.size(); i++) {
            Marker marker = markerWaypoints.get(i);
            if (i == 0) {
                LatLng latLng1 = markerWaypoints.get(i).getPosition();
                LatLng latLng2 = markerWaypoints.get(i + 1).getPosition();
                double azimuthAngle = MapCalculationLogLatDistance.getAzimuthAngle(latLng1.latitude, latLng1.longitude, latLng2.latitude, latLng2.longitude);
                marker.setPeriod(i + 1);
                marker.setSnippet(azimuthAngle + "");
                getCheckStartMarkerView(getContext(), marker, currenPosition, azimuthAngle);
            } else if (i == markerWaypoints.size() - 1) {//结尾
                getUnCheckEndWaypointMarkerView(getContext(), marker, i + 1, currenPosition + 1, 0);
                marker.setPeriod(i + 1);
                marker.setSnippet(0 + "");
            } else {
                LatLng latLng1 = markerWaypoints.get(i).getPosition();
                LatLng latLng2 = markerWaypoints.get(i + 1).getPosition();
                double azimuthAngle = MapCalculationLogLatDistance.getAzimuthAngle(latLng1.latitude, latLng1.longitude, latLng2.latitude, latLng2.longitude);
                marker.setPeriod(i + 1);
                marker.setSnippet(azimuthAngle + "");
                getUnCheckWaypointMarkerView(getContext(), marker, i + 1, currenPosition + 1, azimuthAngle);
            }
        }
    }

    /**
     * 反转带加号的航线航点 Marker
     */
    public void reversalPlusSignWayPointMarker() {
        for (int i = 0; i < markerMiddleWaypoints.size(); i++) {
            Marker marker = markerMiddleWaypoints.get(i);
            marker.setPeriod(i + 1);
        }
    }


    /**
     * 开始拖动
     *
     * @param marker
     */
    public void dragWaypointMarkerStart(Marker marker) {
        int clickPosition = marker.getPeriod();
        int totalSize = latLngDataWaypoints.size();
        int currentPosition = currentMarker.getPeriod();
        marker.setPosition(latLngDataWaypoints.get(clickPosition - 1));
        if (clickPosition != currentPosition) {
            changeCheckWaypointCurrentMarkerStatusColor(getContext(), totalSize, marker, clickPosition);
            changeUnCheckWaypointBeforeMarkerStatusColor(getContext(), totalSize, currentMarker, currentPosition);
        }
    }

    /**
     * 地图点击
     *
     * @param latLng
     */
    public void onMapClick(LatLng latLng) {
        //1 航线的航点经纬度点
        currentLatLng = latLng;
        latLngDataWaypoints.add(latLng);
        //2 航线的Marker点
        if (currentMarker != null) {
            if (currentMarker.getObject() != null) {
                int currentPosition = currentMarker.getPeriod();//3
                int totalSize = markerWaypoints.size();//4
                if (currentPosition <= markerWaypoints.size()) {
                    changeUnCheckWaypointBeforeMarkerStatusColor(getContext(), totalSize, currentMarker, currentPosition);
                }
            }
        }
        currentMarker = addWayPointMarker(getContext(), latLngDataWaypoints, latLng, latLngDataWaypoints.size());
        markerWaypoints.add(currentMarker);
        //3 航点的连线
        Polyline polyline = MapCreateManager.getMapManager().drawTwoPointLine(latLngDataWaypoints);
        if (polyline != null) {
            currentPolyline = polyline;
            polylineWaypoints.add(currentPolyline);
        }
        //4 航线两点之间的中心点
        Marker plusSignMarker = addMiddleMarker(getContext(), latLngDataWaypoints, latLngDataWaypoints.size() - 1);
        if (plusSignMarker != null) {
            currentMiddleMarker = plusSignMarker;
            markerMiddleWaypoints.add(currentMiddleMarker);
        }
    }

    /**
     * marker点 点击
     *
     * @param marker
     * @param type
     */
    public void onMarkerClick(Marker marker, int type) {
        if (currentMarker != null && currentMarker == marker) {
            return;
        }
        if (type == MapMarkerTypeConstant.MARKER_TYPE_WAYPOINT) { //航点 业务逻辑处理
            //查看照片
            int clickPosition = marker.getPeriod();
            int currentPosition = currentMarker.getPeriod();
            if (clickPosition != currentPosition) {
                int totalSize = latLngDataWaypoints.size();
                changeCheckWaypointCurrentMarkerStatusColor(getContext(), totalSize, marker, clickPosition);
                changeUnCheckWaypointBeforeMarkerStatusColor(getContext(), totalSize, currentMarker, currentPosition);
                currentMarker = marker;

            } else { //点击当前点不做处理
                LogUtil.e("Marker", "航点--------------" + currentPosition);
            }
        }
        if (type == MapMarkerTypeConstant.MARKER_TYPE_WAYPOINT_ADD_MIDDLE) { //增加点
            int clickPosition = marker.getPeriod();//Add_Marker  id =1;
            currentMarker = clickMiddleAddMarkerWaypoint(getContext(), latLngDataWaypoints, markerWaypoints,
                    polylineWaypoints, markerMiddleWaypoints, currentMarker, marker, clickPosition);
        }
    }

    /**
     * 添加航点
     *
     * @param context
     * @param latLngs
     * @param size
     * @return
     */
    public Marker addWayPointMarker(Context context, List<LatLng> latLngs, LatLng currentLatLng, int size) {
        if (size == 1) {
            //只有一个点
            return getCheckStartMarkerView(context, currentLatLng, 0);
        } else {
            if (size == 2) {//超过一个点时 起始点变化
                Marker startMarker = markerWaypoints.get(0);
                double beforeCurrentlatLngAngle = MapCalculationLogLatDistance.getAzimuthAngle(startMarker.getPosition().latitude, startMarker.getPosition().longitude, currentLatLng.latitude, currentLatLng.longitude);
                startMarker.setSnippet(beforeCurrentlatLngAngle + "");
                startMarker.setIcon(getUnCheckStartMarkerView(context, beforeCurrentlatLngAngle));
            } else { // size>1
                //倒数第二位
                LatLng beforeCurrentlatLng = latLngs.get(size - 2);
                //角度
                double beforeCurrentlatLngAngle = MapCalculationLogLatDistance.getAzimuthAngle(beforeCurrentlatLng.latitude, beforeCurrentlatLng.longitude, currentLatLng.latitude, currentLatLng.longitude);
                Marker cMarker = markerWaypoints.get(size - 2);
                cMarker.setSnippet(beforeCurrentlatLngAngle + "");//marker 设置角度 beforeCurrentlatLngAngle
                cMarker.setIcon(getUnCheckWaypointMarkerView(context, size - 1, beforeCurrentlatLngAngle));
            }
            return getEndWaypointMarkerView(context, currentLatLng, size);
        }
    }

    /**
     * 点击加号的Marker点 添加航点
     *
     * @param middleMarker
     * @param clickPosition
     */
    public synchronized Marker clickMiddleAddMarkerWaypoint(Context context, List<LatLng> wayPointLatLngs,
                                                            List<Marker> markerWayPointLatLng,
                                                            List<Polyline> polylineWaypoints,
                                                            List<Marker> markerMiddleWaypoints,
                                                            Marker currentMarker,
                                                            Marker middleMarker, int clickPosition) {


        int totalSize = wayPointLatLngs.size();
        //之前选中的航点
        int currentPosition = currentMarker.getPeriod();
        //未选中 最后一个点
        changeUnCheckWaypointBeforeMarkerStatusColor(getContext(), totalSize, currentMarker, currentPosition);
        //todo 1
        wayPointLatLngs.add(clickPosition, middleMarker.getPosition());

        //当前加号的点变成航点 不可能是起始点
        LatLng lasterLatLng = wayPointLatLngs.get(clickPosition + 1);
        //当前点击的带加号+的航点变航点
        Marker wayPointMarker = clickMiddleMarkerAddWaypointMarker(getContext(), middleMarker.getPosition(), lasterLatLng, clickPosition + 1);
        markerWayPointLatLng.add(clickPosition, wayPointMarker);

        //todo 2
        //删除旧add marker点
        middleMarker.remove();
        markerMiddleWaypoints.remove(clickPosition - 1);
        //航线两点之间的中心点
        //点击航点与前面航点的中点
        LatLng beforeLatLng1 = wayPointLatLngs.get(clickPosition - 1);
        LatLng middleLatlng = wayPointLatLngs.get(clickPosition);
        LatLng latterLatLng2 = wayPointLatLngs.get(clickPosition + 1);
        //相邻的上一个航点方位角 1 判断是否是起始点和中间的航点
        if (clickPosition == 1) {//起始点
            Marker waypointMarker = markerWaypoints.get(clickPosition - 1);
            double azimuthAngle = MapCalculationLogLatDistance.getAzimuthAngle(beforeLatLng1.latitude, beforeLatLng1.longitude, middleLatlng.latitude, middleLatlng.longitude);
            waypointMarker.setIcon(getUnCheckStartMarkerView(context, azimuthAngle));
            waypointMarker.setSnippet(azimuthAngle + "");
        } else {
            Marker waypointMarker = markerWaypoints.get(clickPosition - 1);
            double azimuthAngle = MapCalculationLogLatDistance.getAzimuthAngle(beforeLatLng1.latitude, beforeLatLng1.longitude, middleLatlng.latitude, middleLatlng.longitude);
            waypointMarker.setIcon(getUnCheckWaypointMarkerView(context, clickPosition, azimuthAngle));
            waypointMarker.setSnippet(azimuthAngle + "");
        }
        Marker markerMiddleWayPoint1 = addMiddleMarker(getContext(), beforeLatLng1, middleLatlng, clickPosition);
        markerMiddleWaypoints.add(clickPosition - 1, markerMiddleWayPoint1);
        //点击航点与后面航点的中点
        Marker markerMiddleWayPoint2 = addMiddleMarker(getContext(), middleLatlng, latterLatLng2, clickPosition + 1);
        markerMiddleWaypoints.add(clickPosition, markerMiddleWayPoint2);
        //todo 3 改变线条 删除旧航线
        Polyline clickPolyline = polylineWaypoints.get(clickPosition - 1);
        //画出 航点与前面航点的连线
        List<LatLng> latLngs = new ArrayList<>();
        latLngs.add(beforeLatLng1);
        latLngs.add(middleLatlng);
        clickPolyline.setPoints(latLngs);
        //画出 航点与后面航点的连线
        Polyline latterPolyline = MapCreateManager.getMapManager().drawTwoPointBlueLine(middleLatlng, latterLatLng2);
        polylineWaypoints.add(clickPosition, latterPolyline);
        //改变航点的序号 排序
        ThreadUtil.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                sortWayPointMarkerByClickPosition(context, clickPosition, markerWayPointLatLng);
                sortAddSignWayPointMarkerByClickPosition(clickPosition, markerMiddleWaypoints);
            }
        });
        return wayPointMarker;
    }


    /**
     * 航点开始拖动
     *
     * @param marker
     */
    public void differentMarkerTypeDragStart(Marker marker) {
        if (marker.getObject() != null) {
            try {
                //marker点的类型 常量类 MapMarkerTypeConstant
                int type = (int) marker.getObject();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_WAYPOINT) { //航点 业务逻辑处理
                    dragWaypointMarkerStart(marker);
                }
                if (type == MapMarkerTypeConstant.MARKER_TYPE_WAYPOINT_ADD_MIDDLE) { //增加点
                    int clickPosition = marker.getPeriod();
                    //改变连线
                    LatLng gragLatLng = marker.getPosition();
                    LatLng latLng1 = latLngDataWaypoints.get(clickPosition - 1);
                    LatLng latLng2 = latLngDataWaypoints.get(clickPosition);
                    List<LatLng> latLngs = new ArrayList<>();
                    latLngs.add(latLng1);
                    latLngs.add(gragLatLng);
                    latLngs.add(latLng2);
                    Polyline polyline = polylineWaypoints.get(clickPosition - 1);
                    polyline.setPoints(latLngs);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 航点拖动中
     *
     * @param marker
     */
    public void differentMarkerTypeDrag(Marker marker) {
        if (marker.getObject() != null) {
            try {
                //marker点的类型 常量类 MapMarkerTypeConstant
                int type = (int) marker.getObject();
                int clickPosition = marker.getPeriod();
                int totalSize = latLngDataWaypoints.size();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_WAYPOINT) { //航点 业务逻辑处理
                    if (clickPosition == 1) {
                        dragStartWayPointMarkerUpdateData(getContext(), marker, clickPosition);
                    } else {
                        if (clickPosition == totalSize) {
                            dragEndWayPointMarkerUpdateData(getContext(), marker, clickPosition);
                        } else {
                            dragWayPointMarkerUpdateData(getContext(), marker, clickPosition);
                        }
                    }
                    currentMarker = marker;
                }
                if (type == MapMarkerTypeConstant.MARKER_TYPE_WAYPOINT_ADD_MIDDLE) { //增加点
                    dragPlusSignMarker(marker);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 拖动结束
     *
     * @param marker
     */
    public void differentMarkerTypeDragEnd(Marker marker) {
        if (marker.getObject() != null) {
            try {
                //marker点的类型 常量类 MapMarkerTypeConstant
                int type = (int) marker.getObject();
                int clickPosition = marker.getPeriod();
                int totalSize = latLngDataWaypoints.size();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_WAYPOINT) { //航点 业务逻辑处理
                    //查看照片
                    if (clickPosition == 1) {
                        dragStartWayPointMarkerUpdateData(getContext(), marker, clickPosition);
                    } else {
                        if (clickPosition == totalSize) {
                            dragEndWayPointMarkerUpdateData(getContext(), marker, clickPosition);
                        } else {
                            dragWayPointMarkerUpdateData(getContext(), marker, clickPosition);
                        }
                    }
                    currentMarker = marker;
                }
                if (type == MapMarkerTypeConstant.MARKER_TYPE_WAYPOINT_ADD_MIDDLE) { //增加点
                    dragEndPlusSignMarker(marker);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 结束拖动带加号的航点
     *
     * @param marker
     */
    private void dragPlusSignMarker(Marker marker) {
        int clickPosition = marker.getPeriod();
        //改变连线
        LatLng gragLatLng = marker.getPosition();
        LatLng latLng1 = latLngDataWaypoints.get(clickPosition - 1);
        LatLng latLng2 = latLngDataWaypoints.get(clickPosition);
        //第一条线
        List<LatLng> latLngs = new ArrayList<>();
        latLngs.add(latLng1);
        latLngs.add(gragLatLng);
        latLngs.add(latLng2);
        Polyline polyline = polylineWaypoints.get(clickPosition - 1);
        polyline.setPoints(latLngs);


    }

    /**
     * 拖动带加号的航点
     *
     * @param marker
     */
    private void dragEndPlusSignMarker(Marker marker) {
        int clickPosition = marker.getPeriod();
        //拖动点变航点
        currentMarker = clickMiddleAddMarkerWaypoint(getContext(),
                latLngDataWaypoints, markerWaypoints,
                polylineWaypoints, markerMiddleWaypoints,
                currentMarker, marker, clickPosition);

    }


    /**
     * 拖动的是中间点
     * 改变航线数据 不包括起始点和结束点
     *
     * @param context
     * @param dragMarker
     * @param clickPosition
     */
    public void dragWayPointMarkerUpdateData(Context context, Marker dragMarker, int clickPosition) {
        // 索引 从0开始
        int prevPosition = clickPosition - 2;
        LatLng dragLatLng = dragMarker.getPosition();
        //航点经纬度更新
        latLngDataWaypoints.set(clickPosition - 1, dragLatLng);
        //再次计算带加号的中点
        LatLng latLng1 = latLngDataWaypoints.get(prevPosition);
        LatLng latLng2 = latLngDataWaypoints.get(clickPosition);
        //拖动点的航向
        double dragAzimuthAngle = MapCalculationLogLatDistance.getAzimuthAngle(dragLatLng.latitude, dragLatLng.longitude, latLng2.latitude, latLng2.longitude);
        dragMarker.setIcon(getCheckWaypointMarkerView(context, clickPosition, dragAzimuthAngle));
        dragMarker.setSnippet(dragAzimuthAngle + "");
        //相邻的上一个的航向
        if (prevPosition == 0) {
            Marker preWaypointMarker = markerWaypoints.get(prevPosition);
            double azimuthAngle = MapCalculationLogLatDistance.getAzimuthAngle(latLng1.latitude, latLng1.longitude, dragLatLng.latitude, dragLatLng.longitude);
            preWaypointMarker.setIcon(getUnCheckStartMarkerView(context, azimuthAngle));
            preWaypointMarker.setSnippet(azimuthAngle + "");
        } else {
            Marker preWaypointMarker = markerWaypoints.get(prevPosition);
            double azimuthAngle = MapCalculationLogLatDistance.getAzimuthAngle(latLng1.latitude, latLng1.longitude, dragLatLng.latitude, dragLatLng.longitude);
            preWaypointMarker.setIcon(getUnCheckWaypointMarkerView(context, clickPosition - 1, azimuthAngle));
            preWaypointMarker.setSnippet(azimuthAngle + "");
        }
        //计算第一个中点的坐标
        LatLng middleLatLng1 = MapCalculationLogLatDistance.getMiddleLatLng(latLng1, dragLatLng);
        //计算第二个中点的坐标
        LatLng middleLatLng2 = MapCalculationLogLatDistance.getMiddleLatLng(dragLatLng, latLng2);
        //getMiddlePlusSignMarkerView
        int middlePosition1 = prevPosition;
        int middlePosition2 = clickPosition - 1;
        Marker middleMarker1 = markerMiddleWaypoints.get(middlePosition1);
        Marker middleMarker2 = markerMiddleWaypoints.get(middlePosition2);
        middleMarker1.setPosition(middleLatLng1);
        middleMarker2.setPosition(middleLatLng2);
        double distance1 = MapCalculationLogLatDistance.GetDistanceTwo(latLng1.longitude, latLng1.latitude, dragLatLng.longitude, dragLatLng.latitude);
        double distance2 = MapCalculationLogLatDistance.GetDistanceTwo(dragLatLng.longitude, dragLatLng.latitude, latLng2.longitude, latLng2.latitude);
        middleMarker1.setIcon(getMiddlePlusSignMarkerView(context, distance1));
        middleMarker2.setIcon(getMiddlePlusSignMarkerView(context, distance2));
        //连线
        List<LatLng> latLngs1 = new ArrayList<>();
        latLngs1.add(latLng1);
        latLngs1.add(dragLatLng);
        polylineWaypoints.get(middlePosition1).setPoints(latLngs1);
        List<LatLng> latLngs2 = new ArrayList<>();
        latLngs2.add(dragLatLng);
        latLngs2.add(latLng2);
        polylineWaypoints.get(middlePosition2).setPoints(latLngs2);

    }

    /**
     * 拖动的是起始点
     *
     * @param context
     * @param waypointMarker
     * @param clickPosition
     */
    public void dragStartWayPointMarkerUpdateData(Context context, Marker waypointMarker, int clickPosition) {
        if (clickPosition == 1) {
            LatLng dragLatLng = waypointMarker.getPosition();
            //航点经纬度更新
            latLngDataWaypoints.set(clickPosition - 1, dragLatLng);
            //再次计算中点
            LatLng latLng = latLngDataWaypoints.get(clickPosition);

            //两个经纬度点计算航向
            double azimuthAngle = MapCalculationLogLatDistance.getAzimuthAngle(dragLatLng.latitude, dragLatLng.longitude, latLng.latitude, latLng.longitude);
            waypointMarker.setIcon(getCheckStartMarkerView(context, azimuthAngle));
            waypointMarker.setSnippet(azimuthAngle + "");
            int middlePosition = clickPosition - 1;
            //计算第带加号中点的坐标
            Marker middleMarker = markerMiddleWaypoints.get(middlePosition);
            LatLng middleLatLng = MapCalculationLogLatDistance.getMiddleLatLng(dragLatLng, latLng);
            middleMarker.setPosition(middleLatLng);
            //距离
            double distance = MapCalculationLogLatDistance.GetDistanceTwo(latLng.longitude, latLng.latitude, dragLatLng.longitude, dragLatLng.latitude);
            middleMarker.setIcon(getMiddlePlusSignMarkerView(context, distance));
            //连线
            List<LatLng> latLngs1 = new ArrayList<>();
            latLngs1.add(latLng);
            latLngs1.add(dragLatLng);
            polylineWaypoints.get(middlePosition).setPoints(latLngs1);

        }
    }

    public void dragEndWayPointMarkerUpdateData(Context context, Marker dragWayointMarker, int clickPosition) {
        int prevPosition = clickPosition - 2;
        LatLng dragLatLng = dragWayointMarker.getPosition();
        //航点经纬度更新
        latLngDataWaypoints.set(clickPosition - 1, dragLatLng);
        //再次计算中点
        LatLng latLng = latLngDataWaypoints.get(prevPosition);
        //两个经纬度点计算航向
        if (prevPosition == 0) {
            Marker preWaypointMarker = markerWaypoints.get(prevPosition);
            double azimuthAngle = MapCalculationLogLatDistance.getAzimuthAngle(latLng.latitude, latLng.longitude, dragLatLng.latitude, dragLatLng.longitude);
            preWaypointMarker.setIcon(getUnCheckStartMarkerView(context, azimuthAngle));
            preWaypointMarker.setSnippet(azimuthAngle + "");
        } else {
            Marker preWaypointMarker = markerWaypoints.get(prevPosition);
            double azimuthAngle = MapCalculationLogLatDistance.getAzimuthAngle(latLng.latitude, latLng.longitude, dragLatLng.latitude, dragLatLng.longitude);
            preWaypointMarker.setIcon(getUnCheckWaypointMarkerView(context, clickPosition - 1, azimuthAngle));
            preWaypointMarker.setSnippet(azimuthAngle + "");
        }
        int middlePosition = prevPosition;
        //计算第带加号中点的坐标
        Marker middleMarker = markerMiddleWaypoints.get(middlePosition);
        LatLng middleLatLng = MapCalculationLogLatDistance.getMiddleLatLng(dragLatLng, latLng);
        middleMarker.setPosition(middleLatLng);
        //距离
        double distance = MapCalculationLogLatDistance.GetDistanceTwo(latLng.longitude, latLng.latitude, dragLatLng.longitude, dragLatLng.latitude);
        middleMarker.setIcon(getMiddlePlusSignMarkerView(context, distance));
        //连线
        List<LatLng> latLngs1 = new ArrayList<>();
        latLngs1.add(latLng);
        latLngs1.add(dragLatLng);
        polylineWaypoints.get(middlePosition).setPoints(latLngs1);
    }

    /**
     * 点击加号Marker点添加航点
     * 还是选中状态
     * 方向对谁下一个航点
     *
     * @param context
     * @param clickLatLng  点击的点
     * @param latterLatLng 对住下一个点
     * @param position     位置
     */
    public Marker clickMiddleMarkerAddWaypointMarker(Context context, LatLng clickLatLng, LatLng latterLatLng, int position) {
        double azimuthAngle = MapCalculationLogLatDistance.getAzimuthAngle(clickLatLng.latitude, clickLatLng.longitude, latterLatLng.latitude, latterLatLng.longitude);
        return getCheckWaypointMarkerView(context, clickLatLng, position, azimuthAngle);
    }

    /**

     */
    /**
     * 航点点击 选中状态
     * * 改变颜色
     * * ####上一个航点变色
     * * //1 如果是最后一个航点
     * * //2 如果是开始航点
     * * //3 如果是中间航点
     * * ####当前点击的航点变色
     * * //1 如果是最后一个航点
     * * //2 如果是开始航点
     * * //3 如果是中间航点
     * 选中状态
     *
     * @param totalSize 航点总数
     * @param position  从1开始 需要改变的点
     */
    public void changeCheckWaypointCurrentMarkerStatusColor(Context context, int totalSize, Marker marker, int position) {
        if (position == 1) {
            double azimuthAngle = Double.parseDouble(marker.getSnippet());
            marker.setIcon(getCheckStartMarkerView(context, azimuthAngle));
        } else if (position == totalSize) {
            marker.setIcon(getCheckEndWaypointMarkerView(context, position));
        } else {
            double azimuthAngle = Double.parseDouble(marker.getSnippet());
            marker.setIcon(getCheckWaypointMarkerView(context, position, azimuthAngle));
        }
    }

    /**
     * 航点点击 未选中状态
     *
     * @param totalSize
     * @param marker
     * @param position
     */
    public void changeUnCheckWaypointBeforeMarkerStatusColor(Context context, int totalSize, Marker marker, int position) {
        LogUtil.e("设置", position + "-------------------------" + totalSize);
        if (position == 1) {//起始点
            double azimuthAngle = Double.parseDouble(marker.getSnippet());
            marker.setIcon(getUnCheckStartMarkerView(context, azimuthAngle));
        } else if (position == totalSize) { //最后一个点
            marker.setIcon(getUnCheckEndWaypointMarkerView(context, position));
        } else { //中间点
            double azimuthAngle = Double.parseDouble(marker.getSnippet());
            marker.setIcon(getUnCheckWaypointMarkerView(context, position, azimuthAngle));
        }
    }


    public Marker addMiddleMarker(Context context, List<LatLng> latLngs, int size) {
        if (latLngs.size() > 1) {
            LatLng latLng1 = latLngs.get(latLngs.size() - 1);
            LatLng latLng2 = latLngs.get(latLngs.size() - 2);
            return addMiddleMarker(context, latLng1, latLng2, size);
        }
        return null;
    }

    /**
     * 添加中间的带加号的航点
     *
     * @param context
     * @param beforeLatLng
     * @param latterLatLng
     * @param position
     * @return
     */
    public Marker addMiddleMarker(Context context, LatLng beforeLatLng, LatLng latterLatLng, int position) {
        double distance = MapCalculationLogLatDistance.GetDistanceTwo(beforeLatLng.longitude, beforeLatLng.latitude, latterLatLng.longitude, latterLatLng.latitude);
        LatLng middleLatLng = MapCalculationLogLatDistance.getMiddleLatLng(beforeLatLng, latterLatLng);
        return getMiddlePlusSignMarkerView(context, middleLatLng, position, distance);
    }

    /**
     * 增加带加号航点的Marker按钮
     * 带加号的
     *
     * @param context
     * @param latLng
     * @param position
     * @param distance
     * @return
     */
    public Marker getMiddlePlusSignMarkerView(Context context, LatLng latLng, int position, double distance) {
        DroneMiddlePointMarkerBinding middlePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_middle_point_marker, null, false);
        DecimalFormat decimalFormat = new DecimalFormat("###0.0");
        middlePointMarkerBinding.tvMiddlePoint.setText(decimalFormat.format(distance) + "m");
        MarkerOptions markerOptions = new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromView(middlePointMarkerBinding.getRoot())).position(latLng)
                .draggable(true).anchor(0.5f, 0.7f);//第几个点 从1开始
        Marker marker = MapCreateManager.getMapManager().getaMap().addMarker(markerOptions);
        marker.setSnippet(0 + "");
        marker.setZIndex(10);
        marker.setPeriod(position);
        marker.setObject(MapMarkerTypeConstant.MARKER_TYPE_WAYPOINT_ADD_MIDDLE);
        return marker;
    }
    /**
     * 起始点 选中状态
     * 只有起始点一个点
     *
     * @return
     */
    public Marker getCheckStartMarkerView(Context context, LatLng latLng, double azimuthAngle) {
        DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
        dronePointMarkerBinding.tvRotationPoint.setText("S");
        dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#FFFFFF"));
        dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round_blue));
        dronePointMarkerBinding.ivRotationDrone.setImageDrawable(null);
        dronePointMarkerBinding.ivRotationDrone.setRotation((float) azimuthAngle);
        MarkerOptions markerOptions = new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot())).position(latLng)
                .draggable(true).anchor(0.5f, 0.5f)
                .period(1);
        Marker marker = MapCreateManager.getMapManager().getaMap().addMarker(markerOptions);
        marker.setObject(MapMarkerTypeConstant.MARKER_TYPE_WAYPOINT);
        marker.setSnippet(azimuthAngle + "");
        marker.setZIndex(50);
        return marker;
    }

    /**
     * 起始点 未中状态
     *
     * @return
     */
    public BitmapDescriptor getUnCheckStartMarkerView(Context context, double azimuthAngle) {
        DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
        dronePointMarkerBinding.tvRotationPoint.setText("S");
        dronePointMarkerBinding.ivRotationDrone.setImageDrawable(context.getResources().getDrawable(R.drawable.drone_point));
        dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#00BCD4"));
        dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round));
        dronePointMarkerBinding.ivRotationDrone.setRotation((float) azimuthAngle);
        return BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot());
    }

    /**
     * 起始点 选中状态
     * 大于一个点
     *
     * @param context
     * @return
     */
    public BitmapDescriptor getCheckStartMarkerView(Context context, double azimuthAngle) {
        DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
        dronePointMarkerBinding.tvRotationPoint.setText("S");
        dronePointMarkerBinding.ivRotationDrone.setImageDrawable(context.getResources().getDrawable(R.drawable.drone_point));
        dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#FFFFFF"));
        dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round_blue));
        dronePointMarkerBinding.ivRotationDrone.setRotation((float) azimuthAngle);
        return BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot());
    }


    public void getCheckStartMarkerView(Context context, Marker marker, int position, double azimuthAngle) {
        MainThread.run(new Runnable() {
            @Override
            public void run() {
                DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
                dronePointMarkerBinding.tvRotationPoint.setText("S");
                dronePointMarkerBinding.ivRotationDrone.setRotation((float) azimuthAngle);
                if (position == 0) { //选中
                    dronePointMarkerBinding.ivRotationDrone.setImageDrawable(context.getResources().getDrawable(R.drawable.drone_point));
                    dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#FFFFFF"));
                    dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round_blue));
                } else {
                    dronePointMarkerBinding.ivRotationDrone.setImageDrawable(context.getResources().getDrawable(R.drawable.drone_point));
                    dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#00BCD4"));
                    dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round));
                }
                marker.setPeriod(1);
                marker.setSnippet(0 + "");
                marker.setZIndex(50);
                marker.setIcon(BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot()));
            }
        });
    }

    /**
     * 中间的航点  选中状态
     *
     * @param context
     * @param size
     * @return
     */
    public Marker getCheckWaypointMarkerView(Context context, LatLng latLng, int size, double azimuthAngle) {
        DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
        dronePointMarkerBinding.ivRotationDrone.setImageDrawable(context.getResources().getDrawable(R.drawable.drone_point));
        dronePointMarkerBinding.ivRotationDrone.setRotation((float) azimuthAngle);
        dronePointMarkerBinding.tvRotationPoint.setText(size + "");
        dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#FFFFFF"));
        dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round_blue));
        MarkerOptions markerOptions = new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot())).position(latLng)
                .draggable(true).anchor(0.5f, 0.5f)
                .period(size);
        Marker marker = MapCreateManager.getMapManager().getaMap().addMarker(markerOptions);
        marker.setSnippet(azimuthAngle + "");
        marker.setObject(MapMarkerTypeConstant.MARKER_TYPE_WAYPOINT);
        return marker;
    }

    /**
     * 中间航点  点击选中状态
     *
     * @param context
     * @param size
     * @return
     */
    public BitmapDescriptor getCheckWaypointMarkerView(Context context, int size, double azimuthAngle) {
        DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
        dronePointMarkerBinding.ivRotationDrone.setImageDrawable(context.getResources().getDrawable(R.drawable.drone_point));
        dronePointMarkerBinding.tvRotationPoint.setText(size + "");
        dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#FFFFFF"));
        dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round_blue));
        dronePointMarkerBinding.ivRotationDrone.setRotation((float) azimuthAngle);
        return BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot());
    }

    /**
     * 中间航点  未中状态 需要设置角度
     *
     * @param context
     * @param size
     * @return
     */
    public BitmapDescriptor getUnCheckWaypointMarkerView(Context context, int size, double azimuthAngle) {

        DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
        dronePointMarkerBinding.tvRotationPoint.setText(size + "");
        dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#00BCD4"));
        dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round));
        dronePointMarkerBinding.ivRotationDrone.setImageDrawable(context.getResources().getDrawable(R.drawable.drone_point));
        dronePointMarkerBinding.ivRotationDrone.setRotation((float) azimuthAngle);
        return BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot());
    }

    /**
     * 未选中中间航点
     *
     * @param context
     * @param marker
     * @param position
     * @param azimuthAngle
     */
    public void getUnCheckWaypointMarkerView(Context context, Marker marker, int position, double azimuthAngle) {
        MainThread.run(new Runnable() {
            @Override
            public void run() {
                DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
                dronePointMarkerBinding.tvRotationPoint.setText(position + "");
                dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#00BCD4"));
                dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round));
                dronePointMarkerBinding.ivRotationDrone.setImageDrawable(context.getResources().getDrawable(R.drawable.drone_point));
                dronePointMarkerBinding.ivRotationDrone.setRotation((float) azimuthAngle);
                marker.setIcon(BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot()));
                marker.setPeriod(position);
                marker.setZIndex(50);
                marker.setSnippet(azimuthAngle + "");
            }
        });

    }

    public void getUnCheckWaypointMarkerView(Context context, Marker marker, int position, int currentPosition, double azimuthAngle) {
        MainThread.run(new Runnable() {
            @Override
            public void run() {
                DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
                dronePointMarkerBinding.tvRotationPoint.setText(position + "");
                dronePointMarkerBinding.ivRotationDrone.setRotation((float) azimuthAngle);
                if (position == currentPosition) {
                    dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#FFFFFF"));
                    dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round_blue));
                    dronePointMarkerBinding.ivRotationDrone.setImageDrawable(context.getResources().getDrawable(R.drawable.drone_point));
                } else {
                    dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#00BCD4"));
                    dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round));
                    dronePointMarkerBinding.ivRotationDrone.setImageDrawable(context.getResources().getDrawable(R.drawable.drone_point));
                }
                marker.setPeriod(position);
                marker.setSnippet(azimuthAngle + "");
                marker.setIcon(BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot()));
            }
        });

    }

    /**
     * 不需要设置角度
     *
     * @param context
     * @param size
     * @return
     */
    public BitmapDescriptor getUnCheckWaypointMarkerView(Context context, int size) {
        DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
        dronePointMarkerBinding.tvRotationPoint.setText(size + "");
        dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#00BCD4"));
        dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round));
        dronePointMarkerBinding.ivRotationDrone.setImageDrawable(context.getResources().getDrawable(R.drawable.drone_point));
        return BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot());
    }


    /**
     * 最后的航点  选中状态
     *
     * @param context
     * @param position
     * @return
     */
    public Marker getEndWaypointMarkerView(Context context, LatLng latLng, int position) {
        DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
        dronePointMarkerBinding.tvRotationPoint.setText(position + "");
        dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#FFFFFF"));
        dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round_blue));
        dronePointMarkerBinding.ivRotationDrone.setImageDrawable(null);
        MarkerOptions markerOptions = new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot())).position(latLng)
                .draggable(true).anchor(0.5f, 0.5f)
                .period(position);
        Marker marker = MapCreateManager.getMapManager().getaMap().addMarker(markerOptions);
        marker.setSnippet(0 + "");
        marker.setZIndex(50);
        marker.setPeriod(position);
        marker.setObject(MapMarkerTypeConstant.MARKER_TYPE_WAYPOINT);
        return marker;
    }

    /**
     * 最后的航点  选中状态
     *
     * @param context
     * @param size
     * @return
     */
    public BitmapDescriptor getCheckEndWaypointMarkerView(Context context, int size) {
        DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
        dronePointMarkerBinding.tvRotationPoint.setText(size + "");
        dronePointMarkerBinding.ivRotationDrone.setImageDrawable(null);
        dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#FFFFFF"));
        dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round_blue));
        return BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot());
    }

    /**
     * 最后一个点 未选中
     *
     * @param context
     * @param marker
     * @param position
     * @param azimuthAngle
     */
    public void getUnCheckEndWaypointMarkerView(Context context, Marker marker, int position, double azimuthAngle) {
        MainThread.run(new Runnable() {
            @Override
            public void run() {
                DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
                dronePointMarkerBinding.tvRotationPoint.setText(position + "");
                dronePointMarkerBinding.ivRotationDrone.setImageDrawable(null);
                dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#00BCD4"));
                dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round));
                dronePointMarkerBinding.ivRotationDrone.setRotation((float) azimuthAngle);
                marker.setIcon(BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot()));
                marker.setPeriod(position);
                marker.setZIndex(50);
                marker.setSnippet(azimuthAngle + "");
            }
        });
    }

    /**
     * 最后一个点 未选中
     *
     * @param context
     * @param marker
     * @param position
     * @param azimuthAngle
     */
    public void getUnCheckEndWaypointMarkerView(Context context, Marker marker, int position, int currentPosition, double azimuthAngle) {
        MainThread.run(new Runnable() {
            @Override
            public void run() {
                DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
                dronePointMarkerBinding.tvRotationPoint.setText(position + "");
                dronePointMarkerBinding.ivRotationDrone.setRotation((float) azimuthAngle);
                if (position == currentPosition) {
                    dronePointMarkerBinding.ivRotationDrone.setImageDrawable(null);
                    dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#FFFFFF"));
                    dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round_blue));
                } else {
                    dronePointMarkerBinding.ivRotationDrone.setImageDrawable(null);
                    dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#00BCD4"));
                    dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round));
                }
                marker.setPeriod(position);
                marker.setSnippet(azimuthAngle + "");
                marker.setZIndex(50);
                marker.setIcon(BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot()));
            }
        });
    }

    /**
     * 最后的航点  未选中状态
     *
     * @param context
     * @param size
     * @return
     */
    public BitmapDescriptor getUnCheckEndWaypointMarkerView(Context context, int size) {
        DronePointMarkerBinding dronePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_point_marker, null, false);
        dronePointMarkerBinding.tvRotationPoint.setText(size + "");
        dronePointMarkerBinding.ivRotationDrone.setImageDrawable(null);
        dronePointMarkerBinding.tvRotationPoint.setTextColor(Color.parseColor("#00BCD4"));
        dronePointMarkerBinding.tvRotationPoint.setBackground(context.getResources().getDrawable(R.drawable.bg_rc_round));
        return BitmapDescriptorFactory.fromView(dronePointMarkerBinding.getRoot());
    }


    public BitmapDescriptor getMiddlePlusSignMarkerView(Context context, double distance) {
        DroneMiddlePointMarkerBinding middlePointMarkerBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.drone_middle_point_marker, null, false);
        DecimalFormat decimalFormat = new DecimalFormat("###0.0");
        middlePointMarkerBinding.tvMiddlePoint.setText(decimalFormat.format(distance) + "m");
        return BitmapDescriptorFactory.fromView(middlePointMarkerBinding.getRoot());
    }

    /**
     * 从当前索引开始 ，改变后面marker点的序号
     */
    public void sortWayPointMarkerByClickPosition(Context context, int clickPosition, List<Marker> markerLatLng) {
        int statPosition = clickPosition + 1;//2
        for (int i = statPosition; i < markerLatLng.size(); i++) {
            Marker marker = markerLatLng.get(i);// 2
            if (marker.getObject() != null) {
                int type = (int) marker.getObject();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_WAYPOINT) { //航点 业务逻辑处理
                    double azimuthAngle = Double.parseDouble(marker.getSnippet());
                    marker.setPeriod(i + 1);
                    if (i == markerLatLng.size() - 1) {
                        getUnCheckEndWaypointMarkerView(context, marker, i + 1, azimuthAngle);
                    } else {
                        getUnCheckWaypointMarkerView(context, marker, i + 1, azimuthAngle);
                    }
                }
            }
        }
    }

    /**
     * 带加号的航点排序
     *
     * @param clickPosition
     * @param markerLatLng
     */
    public void sortAddSignWayPointMarkerByClickPosition(int clickPosition, List<Marker> markerLatLng) {
        int statPosition = clickPosition - 1;
        for (int i = statPosition; i < markerLatLng.size(); i++) {
            Marker marker = markerLatLng.get(i);// 2
            if (marker.getObject() != null) {
                int type = (int) marker.getObject();
                if (type == MapMarkerTypeConstant.MARKER_TYPE_WAYPOINT_ADD_MIDDLE) { //航点 业务逻辑处理
                    marker.setPeriod(i + 1);
                }
            }
        }
    }

    /**
     * 销毁
     */
    public void onDestroy() {
        if (currentMarker != null) {
            currentMarker.remove();
        }
        if (currentMiddleMarker != null) {
            currentMiddleMarker.remove();
        }
        latLngDataWaypoints.clear();
        if (markerWaypoints != null) {
            for (Marker marker : markerWaypoints) {
                marker.remove();
            }
            markerWaypoints.clear();
        }

        if (markerMiddleWaypoints != null) {
            for (Marker marker : markerMiddleWaypoints) {
                marker.remove();
            }
            markerMiddleWaypoints.clear();
        }

        if (polylineWaypoints != null) {
            for (Polyline polyline : polylineWaypoints) {
                polyline.remove();
            }
            polylineWaypoints.clear();
        }
        if (currentPolyline != null) {
            currentPolyline.remove();
        }

    }
}
